package com.baosight.risk.web.fltd;

import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baosight.risk.api.fltd.FltdTaskService;
import com.baosight.risk.api.rifi.RifiRiskFinishService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.service.fltd.entity.FltdTask;
import com.baosight.risk.service.fltd.vo.FltdTaskVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 作者： lishaoqing
 * 时间： 2019年07月15日
 * 描述： FltdTaskController
 * 版本: v1.0
 */
@Api(value = "工作流待办", description = "工作流待办", produces = MediaType.APPLICATION_JSON)
@Slf4j
@RestController
@RequestMapping("/fltd/task")
public class FltdTaskController extends BaseController {

    @Autowired
    private FltdTaskService fltdTaskService;

    @Autowired
    private RifiRiskFinishService rifiRiskFinishService;

    @ApiOperation("根据重点风险id查询待办")
    @GetMapping("task")
    public AjaxResult task(String todoGuid) {
        return AjaxResult.success("查询成功", fltdTaskService.selectTaskByTodoGuid(todoGuid));
    }

    @ApiOperation("获取待办列表")
    @GetMapping("taskList")
    public AjaxResult taskList(
            @RequestParam(value = "queryType") String queryType,
            @RequestParam(value = "bizDesc", required = false) String bizDesc,
            @RequestParam(value = "currUserName", required = false) String currUserName,
            @RequestParam(value = "currUserCode", required = false) String currUserCode
    ) {
        startPage();
        FltdTaskVO param = new FltdTaskVO();
        param.setBizDesc(bizDesc);
        param.setCurrUserName(currUserName);
        param.setCurrUserCode(currUserCode);
        return getResultPage(fltdTaskService.selectTaskList(queryType, param, getUser().getUserCode()));
    }

    @ApiOperation("获取待办条数")
    @GetMapping("taskCount")
    public AjaxResult taskCount() {
        return AjaxResult.success("查询成功", fltdTaskService.selectTaskCount(getUser().getUserCode()));
    }

    @ApiOperation("查询所有人的待办")
    @GetMapping("allTask")
    public AjaxResult allTask() {
        return AjaxResult.success("查询成功", fltdTaskService.selectAllTaskList());
    }

    @ApiOperation("获取消息通知列表")
    @GetMapping("messageList")
    public AjaxResult messageList(
            @RequestParam(value = "queryType") String queryType,
            @RequestParam(value = "bizDesc", required = false) String bizDesc,
            @RequestParam(value = "currUserName", required = false) String currUserName,
            @RequestParam(value = "currUserCode", required = false) String currUserCode
    ) {
        startPage();
        FltdTaskVO param = new FltdTaskVO();
        param.setBizDesc(bizDesc);
        param.setCurrUserName(currUserName);
        param.setCurrUserCode(currUserCode);
        return getResultPage(fltdTaskService.selectMessageList(queryType, param, getUser().getUserCode()));
    }

    @ApiOperation("获取消息通知条数")
    @GetMapping("messageCount")
    public AjaxResult messageCount() {
        return AjaxResult.success("查询成功", fltdTaskService.selectMessageCount(getUser().getUserCode()));
    }

    @ApiOperation("获取合并前的待办列表")
    @GetMapping("groupedTask")
    public AjaxResult groupedTask(String flowNo, String nodeCode) {
        return AjaxResult.success("查询成功", fltdTaskService.selectGroupedTask(getUser().getUserCode(), flowNo, nodeCode));
    }

    @ApiOperation("获取当前流程的审批历史")
    @GetMapping("history")
    public AjaxResult history(String procGuid) {
        return AjaxResult.success("查询成功", fltdTaskService.selectHistory(procGuid));
    }

    @ApiOperation("查询指定单位是否已经下发过待办")
    @GetMapping("exist")
    public AjaxResult selectExistTask(String flowNo, String acctYear, String quarterCode, String deptCode) {
        FltdTask task = new FltdTask();
        task.setFlowNo(flowNo);
        task.setAcctYear(acctYear);
        task.setQuarterCode(quarterCode);
        task.setDeptCode(deptCode);
        return AjaxResult.success("查询成功", fltdTaskService.selectExistTask(task));
    }

    @ApiOperation("查询指定重点风险id是否已启动过流程")
    @GetMapping("procId")
    public AjaxResult selectProcId(String flowNo, String taskGuid, String bizGuid) {
        return AjaxResult.success("查询成功", fltdTaskService.selectProcId(flowNo, taskGuid, bizGuid));
    }

    @ApiOperation("批量查询指定重点风险id是否已启动过流程")
    @GetMapping("procIds")
    public AjaxResult selectProcIds(String flowNo, String taskGuid, String bizGuids) {
        return AjaxResult.success("查询成功", fltdTaskService.selectProcIds(flowNo, taskGuid, bizGuids));
    }

    @ApiOperation("查询我的所有任务")
    @GetMapping("ownerTask")
    public AjaxResult ownerTask(
            @RequestParam(value = "bizName", required = false) String bizName,
            @RequestParam(value = "bizDesc", required = false) String bizDesc,
            @RequestParam(value = "acctYear", required = false) String acctYear,
            @RequestParam(value = "quarterCurrent", required = false) String quarterCurrent
    ) {
        startPage("factStartDate", "desc");
        return getResultPage(fltdTaskService.selectOwnerTask(getUser().getUserCode(), bizName, bizDesc, acctYear, quarterCurrent));
    }

    @ApiOperation("查询待办列表，汇总提交校验")
    @GetMapping("checkGroup")
    public AjaxResult checkGroup(FltdTaskVO taskVO) {
        return AjaxResult.success("查询成功", fltdTaskService.checkGroup(taskVO));
    }

    @ApiOperation("查询所有待催办的待办记录")
    @GetMapping("urgentTask")
    public AjaxResult selectUrgentTask() {
        return AjaxResult.success("查询成功", fltdTaskService.selectUrgentTask());
    }

    @ApiOperation("查询所有待催办的待办记录")
    @GetMapping("current")
    public AjaxResult getCurrentTasks(String procGuid, String nodeCode) {
        return AjaxResult.success("查询成功", fltdTaskService.selectCurrentTasks(procGuid, nodeCode));
    }

    @ApiOperation("批量查询所有待催办的待办记录")
    @GetMapping("currents")
    public AjaxResult getCurrents(String procGuids, String nodeCode) {
        return AjaxResult.success("查询成功", fltdTaskService.selectCurrents(procGuids, nodeCode));
    }

    @ApiOperation("批量查询所有待专家组催办的待办记录")
    @GetMapping("batchExpertCurrent")
    public AjaxResult getBatchExpertCurrent(String ids) {
        return AjaxResult.success("查询成功", fltdTaskService.selectBatchExpertTasks(ids));
    }

    @ApiOperation("批量查询所有待催办的待办记录责任人")
    @GetMapping("batchCurrent")
    public AjaxResult getBatchCurrentTasks(String procGuid, String nodeCode) {
        return AjaxResult.success("查询成功", fltdTaskService.selectBatchCurrentTasks(procGuid, nodeCode));
    }

    @ApiOperation("关闭当前待办/消息")
    @GetMapping("closeTask")
    public AjaxResult getCurrentTasks(@RequestParam("todoGuid") String todoGuid) {
        fltdTaskService.closeTask(todoGuid);
        return AjaxResult.success();
    }

    @ApiOperation("批量查询风险结题责任人")
    @GetMapping("batchFinishCurrent")
        public AjaxResult batchFinishCurrent(@RequestParam("ids") String[] ids) {
        return AjaxResult.success("查询成功", rifiRiskFinishService.selectListByIds(ids));
    }

    @ApiOperation("补偿删除oa待办")
    @DeleteMapping("compensateCloseOA")
    public AjaxResult compensateCloseOA() throws Exception{
    	fltdTaskService.compensateClose();
        return AjaxResult.success();
    }
}
